package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.Activity.CoalChemicalActivity;
import com.tianxin.workbao.workBao.Activity.CoalPriceActivity;
import com.tianxin.workbao.workBao.Activity.ContractActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.CtCoaltype;
import com.tianxin.workbao.workBao.dataProvide.ContractProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class ContractCoalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<CtCoaltype> ctCoaltypeList;
    private Context mContext;

    public ContractCoalAdapter(List<CtCoaltype> list, Context context) {
        this.ctCoaltypeList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (ctCoaltypeList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contract_coal_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(ctCoaltypeList.get(position));
            viewHolder.SetViewListener(ctCoaltypeList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        if (ctCoaltypeList.size() > 0) {
            return ctCoaltypeList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIv;
        TextView numberTv;
        TextView timeTv;
        TextView coalNameTv;
        ExpandView expandView;
        TextView statusLabelTv;
        TextView transTv;
        TextView noTransTv;
        Button priceRecordBtn;
        Button chemicalRecordBtn;


        public ViewHolder(View itemView) {
            super(itemView);
            arrowIv = (ImageView) itemView.findViewById(R.id.arrow);
            numberTv = (TextView) itemView.findViewById(R.id.number);
            coalNameTv = (TextView) itemView.findViewById(R.id.coal_name);
            statusLabelTv = (TextView) itemView.findViewById(R.id.status_label);
            transTv = (TextView) itemView.findViewById(R.id.trans);
            noTransTv = (TextView) itemView.findViewById(R.id.no_trans);
            numberTv = (TextView) itemView.findViewById(R.id.number);
            timeTv = (TextView) itemView.findViewById(R.id.time);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            priceRecordBtn = (Button) itemView.findViewById(R.id.price_record_btn);
            chemicalRecordBtn = (Button) itemView.findViewById(R.id.chemical_record_btn);
        }

        public void BindViewData(CtCoaltype coaltype) {
            if (coaltype.getCttype() == 1) {
                statusLabelTv.setText("销售");
            } else {
                statusLabelTv.setText("采购");
            }
            numberTv.setText(coaltype.getContractid() + "");
            timeTv.setText(TimeUtils.parseDateTime(coaltype.getAddtime()));
            coalNameTv.setText(coaltype.getCoaltypename() + "  " + "签订单价：￥" + coaltype.getContractunitprice());
            transTv.setText("已运：" + coaltype.getAlreadysetweight() + "吨");
            noTransTv.setText("未运：" + coaltype.getNosetweight() + "吨");
        }

        public void SetViewListener(final CtCoaltype coaltype) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                arrowIv.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;

                        case R.id.price_record_btn:
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("ctCoalType", coaltype);
                            Intent intent = new Intent(mContext, CoalPriceActivity.class);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                            break;
                        case R.id.chemical_record_btn:
                            Bundle bundle3 = new Bundle();
                            bundle3.putSerializable("ctCoalType", coaltype);
                            Intent intent3 = new Intent(mContext, CoalChemicalActivity.class);
                            intent3.putExtras(bundle3);
                            mContext.startActivity(intent3);
                            break;
                    }
                }
            };
            arrowIv.setOnClickListener(listener);
            priceRecordBtn.setOnClickListener(listener);
            chemicalRecordBtn.setOnClickListener(listener);
        }
    }

    private int mPosition;
    private UpdateListViewListener listener = new UpdateListViewListener() {
        @Override
        public void onRefreshData(Object result) {

        }

        @Override
        public void onLoadMoreData(Object result) {

        }

        @Override
        public void showMessage(String msg) {
            if (msg == "ok") {
                ctCoaltypeList.get(mPosition);
                notifyDataSetChanged();
                Toast.makeText(mContext, "操作成功", Toast.LENGTH_LONG).show();
            } else if (msg.contains("failure")) {
                Toast.makeText(mContext, "操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void showProgress() {
            ContractActivity activity = (ContractActivity) mContext;
            activity.getSweetAlertDialog(activity).showDialog("", 1200);
        }

        @Override
        public void hideProgress() {
            ContractActivity activity = (ContractActivity) mContext;
            activity.getSweetAlertDialog(activity).dismiss();
        }
    };
    private ContractProvide provide;

    public ContractProvide getContractProvide() {
        if (provide == null) {
            provide = new ContractProvide(mContext, listener);
        }
        return provide;
    }

}
